﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilLib;
using System.Text.RegularExpressions;
using static ServerBase.Config.Conf;
using ServerBase.Protocol;
using ServerBase.Config;
using ServerBase.Service;
using UtilPublic.ServerLoger;

namespace ServerPublic.Manager
{
    /// <summary>
    /// 名字管理器
    /// </summary>
    public static class NameMgr
    {
        /// <summary>
        /// 获取名字
        /// </summary>
        /// <param name="gender">性别 1=男性 2=女性</param>
        /// <returns></returns>
        public static string GetName(int gender)
        {
            string name = "无名氏";
            string surname = "";
            string fame = "";
            if (ConfSurname.Count > 0)
            {
                surname = ConfSurnameExt[Util.Random.Next(ConfSurnameExt.Count)];
            }
            if (gender == 1)
            {
                if (ConfMalename.Count > 0)
                {
                    fame = ConfMalenameExt[Util.Random.Next(ConfMalenameExt.Count)];
                }
            }
            else
            {
                if (ConfFemalename.Count > 0)
                {
                    fame = ConfFemalenameExt[Util.Random.Next(ConfFemalenameExt.Count)];
                }
            }
            if (surname.Length + fame.Length > 6)
            {
                name = fame;
            }
            else
            {
                name = surname + fame;
            }
            if (name == "")
            {
                name = "无名氏";
            }
            return name;
        }

        /// <summary>
        /// 获取名字
        /// </summary>
        /// <returns></returns>
        public static string GetName()
        {
            int gender = Util.Random.Next(1, 3);
            return GetName(gender);
        }

        /// <summary>
        /// 判断账号是否合法（只能使用英文和数字）
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static bool CheckAccountLegal(string v)
        {
            string pattern = @"^[A-Za-z0-9]+$";
            var rx = new Regex(pattern);
            return rx.IsMatch(v);
        }

        /// <summary>
        /// 判断密码是否合法（只能使用英文和数字）
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static bool CheckPasswdLegal(string v)
        {
            string pattern = @"^[A-Za-z0-9]+$";
            var rx = new Regex(pattern);
            return rx.IsMatch(v);
        }

        /// <summary>
        /// 判断中文名是否合法
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static bool CheckNameLegal(string v)
        {
            return CheckNameLegalLanguage(v, BaseServerInfo.Language);
        }

        /// <summary>
        /// 判断中文名是否合法
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static bool CheckNameLegalLanguage(string v, ELanguage Language)
        {
            //中文简体 @"^[\u4e00-\u9fa5]+$"
            //韩语 @"^[\uac00-\ud7ff]+$"
            string pattern;
            if (Language == ELanguage.英语)
            {
                pattern = @"^[A-Za-z0-9]+$";
                var rx = new Regex(pattern);
                return rx.IsMatch(v);
            }
            else if (Language == ELanguage.中文简体
                || Language == ELanguage.中文繁体)
            {
                pattern = @"^[A-Za-z0-9\u4e00-\u9fa5]+$";
                var rx = new Regex(pattern);
                return rx.IsMatch(v);
            }
            else if (Language == ELanguage.韩语)
            {
                pattern = @"^[A-Za-z0-9\uac00-\ud7ff]+$";
                var rx = new Regex(pattern);
                return rx.IsMatch(v);
            }
            return true;
        }

        /// <summary>
        /// 判断是否屏蔽字
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static bool CheckBadWords(string v)
        {
            if (ConfBadwordsExt.Contains(v))
            {
                return true;
            }
            return false;
        }

        //检查姓名
        public static EProtocolResult CheckName(string name)
        {
            //名字不能为空
            if (name.Length <= 0 || name == "" || name.IsNullOrEmpty())
            {
                return EProtocolResult.名字长度不符;
            }
            var ByteLength = name.ByteLength();
            if (ByteLength < EGlobalId.昵称长度最小.ValueInt()
                || ByteLength > EGlobalId.昵称长度最大.ValueInt())
            {
                return EProtocolResult.名字长度不符;
            }
            //不能含有非法字符
            if (!CheckNameLegal(name))
            {
                return EProtocolResult.名字含有不合法或者不雅字符;
            }
            //不能含有非法字符
            if (CheckBadWords(name))
            {
                return EProtocolResult.名字含有不合法或者不雅字符;
            }
            return EProtocolResult.成功;
        }

    }
}